package nyist.media.repository;

import nyist.media.entity.ClassRoom;
import nyist.media.enums.RoomType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import javax.persistence.criteria.Predicate;
import java.util.Objects;

/**
 * 教室模块
 *
 * @author : dcf
 * @date : 2019/3/15
 */

public interface ClassRoomRepository extends JpaRepository<ClassRoom, Long>, JpaSpecificationExecutor<ClassRoom> {


    /**
     * 查询教室列表
     *
     * @param keyword     搜索关键字
     * @param type        教室类型
     * @param pageRequest 分页对象
     * @return 教室列表
     */
    default Page<ClassRoom> findAll(String keyword, RoomType type, PageRequest pageRequest) {
        return findAll(((root, query, cb) -> {
            Predicate r = cb.conjunction();
            if (!StringUtils.isEmpty(keyword)) {
                String s = "%" + keyword + "%";
                r = cb.and(r, cb.or(cb.like(root.get("roomNo"), s), cb.like(root.get("address"), s)));
            }
            if (!Objects.isNull(type)) {
                r = cb.and(r, cb.equal(root.get("type"), type));
            }
            return r;
        }), pageRequest);
    }
}

